<?php

class Cabang extends Controller {
	/**
	 * Constructor
	 */
	function Cabang()
	{
		parent::Controller();
		$this->load->model('Cabang_model', '', TRUE);
	}
	
	/**
	 * Inisialisasi variabel untuk $title(untuk id element <body>)
	 */
	var $title = 'cabang';
	
	/**
	 * Memeriksa user state, jika dalam keadaan login akan menampilkan halaman cabang,
	 * jika tidak akan meredirect ke halaman login
	 */
	function index()
	{
		if ($this->session->userdata('login') == TRUE)
		{
			if ($this->session->userdata('id_usergroup') == 6 || $this->session->userdata('id_usergroup') == 1)
			{
				if($this->session->userdata('analisa') == TRUE){
				
					$this->analisa_model->ana_off();
					$this->get_all();
				}
				else {
					$this->get_all();
				}
			}
			else
			{
				redirect('home');
			}
		}
		else
		{
			redirect('login');
		}
	}
	
	/**
	 * Tampilkan semua data cabang
	 */
	function get_all()
	{
		$data['title'] = $this->title;
		$data['h2_title'] = 'Cabang';
		$data['main'] = 'cabang/cabang';
		
		// Load data
		$query = $this->Cabang_model->get_all();
		$cabang = $query->result();
		$num_rows = $query->num_rows();
		
		if ($num_rows > 0)
		{
			// Table
			/*Set table template for alternating row 'zebra'*/
			$tmpl = array( 'table_open'    => '<table border="0" cellpadding="0" cellspacing="0">',
						  'row_alt_start'  => '<tr class="zebra">',
							'row_alt_end'    => '</tr>'
						  );
			$this->table->set_template($tmpl);

			/*Set table heading */
			$this->table->set_empty("&nbsp;");
			$this->table->set_heading('No', 'Cabang', 'Kota', 'Actions');
			$i = 0;
			
			foreach ($cabang as $row)
			{
				$this->table->add_row(++$i, $row->cabang, $row->kota,
										anchor('cabang/view/'.$row->id_cabang,'view',array('class' => 'view')).' '.
										anchor('cabang/update/'.$row->id_cabang,'update',array('class' => 'update')).' '.
										anchor('cabang/delete/'.$row->id_cabang,'hapus',array('class'=> 'delete','onclick'=>"return confirm('Anda yakin akan menghapus data ini?')"))
										);
			}
			$data['table'] = $this->table->generate();
		}
		else
		{
			$data['message'] = 'Tidak ditemukan satupun data cabang!';
		}		
		
		$data['link'] = array('link_add' => anchor('cabang/add/','tambah data', array('class' => 'add'))
								);
		
		// Load view
		$this->load->view('base', $data);
	}
		
	/**
	 * Hapus data cabang
	 */
	function delete($id_cabang)
	{
		$this->Cabang_model->delete($id_cabang);
		$this->session->set_flashdata('message', '1 data cabang berhasil dihapus');
		
		redirect('cabang');
	}
	
	/**
	 * Pindah ke halaman tambah cabang
	 */
	function add()
	{		
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > Tambah Data';
		$data['main'] 		= 'cabang/cabang_form';
		$data['form_action']	= site_url('cabang/add_process');
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
		
		$this->load->view('base', $data);
	}
	
	/**
	 * Proses tambah data cabang
	 */
	function add_process()
	{
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > Tambah Data';
		$data['main'] 		= 'cabang/cabang_form';
		$data['form_action']	= site_url('cabang/add_process');
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
	
		// Set validation rules
		$this->form_validation->set_rules('cabang', 'cabang', 'required|max_length[200]');
		$this->form_validation->set_rules('alamat', 'alamat', 'required|max_length[200]');
		$this->form_validation->set_rules('kota', 'kota', 'required|max_length[50]');
		$this->form_validation->set_rules('phone', 'phone', 'required|max_length[50]');
		$this->form_validation->set_rules('fax', 'fax', 'required|max_length[50]');
		$this->form_validation->set_rules('email', 'email', 'required|max_length[50]');
		
		
		// Jika validasi sukses
		if ($this->form_validation->run() == TRUE)
		{
			// Persiapan data
			$cabang = array('cabang'		=> $this->input->post('cabang'),
							'alamat'		=> $this->input->post('alamat'),
							'kota'			=> $this->input->post('kota'),
							'phone'			=> $this->input->post('phone'),
							'fax'			=> $this->input->post('fax'),
							'email'			=> $this->input->post('email')
						);
			// Proses penyimpanan data di table cabang
			$this->Cabang_model->add($cabang);
		
			$this->session->set_flashdata('message', 'Satu data cabang berhasil disimpan!');
			redirect('cabang/add');
		}
		// Jika validasi gagal
		else
		{		
			$this->load->view('base', $data);
		}	
	}
	
	/**
	 * Pindah ke halaman view cabang
	 */
	function view($id_cabang)
	{
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > view';
		$data['main'] 		= 'cabang/cabang_view';
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
	
		// cari data dari database
		$cabang = $this->Cabang_model->get_cabang_by_id($id_cabang);
				
		// buat session untuk menyimpan data primary key (id_cabang)
		$this->session->set_userdata('id_cabang', $cabang->id_cabang);
		
		// Data untuk mengisi field2 form	
		$data['default']['cabang']		= $cabang->cabang;		
		$data['default']['alamat']		= $cabang->alamat;		
		$data['default']['kota']		= $cabang->kota;		
		$data['default']['phone']		= $cabang->phone;		
		$data['default']['fax']			= $cabang->fax;		
		$data['default']['email']		= $cabang->email;
				
		$this->load->view('base', $data);
	}
	/**
	 * Pindah ke halaman update cabang
	 */
	function update($id_cabang)
	{
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > Update';
		$data['main'] 		= 'cabang/cabang_form';
		$data['form_action']	= site_url('cabang/update_process');
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
	
		// cari data dari database
		$cabang = $this->Cabang_model->get_cabang_by_id($id_cabang);
				
		// buat session untuk menyimpan data primary key (id_cabang)
		$this->session->set_userdata('id_cabang', $cabang->id_cabang);
		
		// Data untuk mengisi field2 form	
		$data['default']['cabang']		= $cabang->cabang;		
		$data['default']['alamat']		= $cabang->alamat;		
		$data['default']['kota']		= $cabang->kota;		
		$data['default']['phone']		= $cabang->phone;		
		$data['default']['fax']			= $cabang->fax;		
		$data['default']['email']		= $cabang->email;
				
		$this->load->view('base', $data);
	}
	
	/**
	 * Proses update data cabang
	 */
	function update_process()
	{
		$data['title'] 			= $this->title;
		$data['h2_title'] 		= 'Cabang > Update';
		$data['main'] 		= 'cabang/cabang_form';
		$data['form_action']	= site_url('cabang/update_process');
		$data['link'] 			= array('link_back' => anchor('cabang','kembali', array('class' => 'back'))
										);
										
		// Set validation rules
		$this->form_validation->set_rules('cabang', 'Cabang', 'required|max_length[200]');
		$this->form_validation->set_rules('alamat', 'alamat', 'required|max_length[200]');
		$this->form_validation->set_rules('kota', 'kota', 'required|max_length[50]');
		$this->form_validation->set_rules('phone', 'phone', 'required|max_length[50]');
		$this->form_validation->set_rules('fax', 'fax', 'required|max_length[50]');
		$this->form_validation->set_rules('email', 'email', 'required|max_length[50]');
		
		// Jika validasi sukses
		if ($this->form_validation->run() == TRUE)
		{
			// Persiapan data
			$cabang = array('cabang'		=> $this->input->post('cabang'),
							'alamat'		=> $this->input->post('alamat'),
							'kota'			=> $this->input->post('kota'),
							'phone'			=> $this->input->post('phone'),
							'fax'			=> $this->input->post('fax'),
							'email'			=> $this->input->post('email')
						);
			$this->Cabang_model->update($this->session->userdata('id_cabang'), $cabang);
			
			$this->session->set_flashdata('message', 'Satu data cabang berhasil diupdate!');
			redirect('cabang');
		}
		// Jika validasi gagal
		else
		{		
			$this->load->view('base', $data);
		}

	}

}

// END cabang Class

/* End of file cabang.php */
/* Location: ./system/application/controllers/cabang.php */